package com.tql.mall.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tql.mall.common.result.R;
import com.tql.mall.common.util.AuthContextHolder;
import com.tql.mall.model.merchant.*;
import com.tql.mall.product.client.impl.ProductDegradeFeignClient;
import com.tql.mall.user.service.MerchantService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Api(description = "商家信息接口")
@RestController
@RequestMapping("/user/merchant")
public class MerchantController{

    @Autowired
    private MerchantService merchantService;

    @Autowired
    private ProductDegradeFeignClient productDegradeFeignClient;

    @ApiOperation(value = "用户注册")
    @PostMapping(value = "/register")
    public R<Object> register(@RequestBody  MerchantRegisterVo merchantRegisterVo){
        String msg = merchantService.register(merchantRegisterVo);
        if (msg == null) return R.ok();
        return R.fail().msg(msg);
    }

    /**
     * 用户登录，脚手架登录接口
     * @param merchantLoginVo
     * @return
     */
    @ApiOperation(value = "用户登录(用于商家管理系统)")
    @PostMapping(value = "/login")
    public R<Object> login(@RequestBody MerchantLoginVo merchantLoginVo){
        MerchantLoginDto merchantLoginDto = merchantService.login(merchantLoginVo);
        if (merchantLoginDto.getMerchant()==null){ //登录失败
            return R.fail().msg(merchantLoginDto.getMsg());
        }
        Map<String,Object> map = new HashMap<>();
        map.put("token",merchantLoginDto.getMerchant().getToken());
        return R.ok(map);//返回token
    }


    /**
     * 脚手架获取用户信息
     * @param token
     * @return
     */
    @ApiOperation(value = "登录,返回用户信息（脚手架要求）")
    @GetMapping("/info")
    public R<Merchant> info(@RequestParam("token") String token, HttpServletRequest request){
        String merchantId = AuthContextHolder.getUserId(request);
        Merchant merchant = merchantService.getById(Long.parseLong(merchantId));
        return R.ok(merchant);
    }

    /**
     * 上传认证信息
     */
    @ApiOperation(value = "上传认证信息")
    @PostMapping("/uploadAuthInfo")
    public R<Void> uploadAuthInfo(@RequestBody Merchant merchant){
        //将图片上传到minio
        Boolean flag = merchantService.uploadAuthInfo(merchant);
        if (flag) return R.ok();
        return R.fail();
    }

    /**
     * 根据id获取商家信息
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id获取商家信息")
    @GetMapping("/getMerchant/{id}")
    public R<Merchant> getMerchantById(@PathVariable Long id){
        return R.ok(merchantService.getById(id));
    }

    @ApiOperation(value = "分页+条件 : 查询商家信息")
    @GetMapping("/getMerchant/page/{pageNum}/{pageSize}")
    public R<Map<String,Object>> getMerchantPage(MerchantQueryVo merchantQueryVo,
                             @ApiParam(name = "pageNum",value = "当前页")
                             @PathVariable Integer pageNum,
                             @ApiParam(name = "pageSize",value = "页大小")
                             @PathVariable Integer pageSize
                             ){
        Page<Merchant> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Merchant> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(merchantQueryVo.getId())){
            queryWrapper.eq("id",merchantQueryVo.getId());
        }
        if (!StringUtils.isEmpty(merchantQueryVo.getAccount())){
            queryWrapper.like("account",merchantQueryVo.getAccount());
        }
        if (!StringUtils.isEmpty(merchantQueryVo.getStatus())){
            queryWrapper.eq("status",merchantQueryVo.getStatus());
        }
        if (!StringUtils.isEmpty(merchantQueryVo.getAuthStatus())){
            queryWrapper.eq("auth_status",merchantQueryVo.getAuthStatus());
        }
        merchantService.page(page,queryWrapper);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",page.getTotal());
        map.put("list",page.getRecords());
        return R.ok(map);
    }


    @ApiOperation(value = "修改商家的锁定状态")
    @PutMapping("/update/status/{id}/{status}")
    public R<Void> updateStatus(@PathVariable Long id, @PathVariable Integer status ){
        Boolean flag = merchantService.updateStatus(id, status);
        if (flag) return R.ok();
        return R.fail();
    }

    @ApiOperation(value = "修改商家的认证状态")
    @PutMapping("/update/authStatus/{id}/{authStatus}")
    public R<Void> updateAuthStatus(@PathVariable Long id, @PathVariable Integer authStatus ){
        Boolean flag = merchantService.updateAuthStatus(id, authStatus);
        if (flag) return R.ok();
        return R.fail();
    }
}

